const OSS = require('ali-oss');
const moment = require('moment');

//const env = process.env.NODE_ENV;

let expirationTime = null; // STS token 过期时间
let expirationTime2 = null; // STS token 过期时间

let client = null; // OSS Client 实例
let videoclient = null;//网校管理员私有属性文件


const bucket = {
	publicfile:'newxuejingimage',//网校管理员公共图片文件
	privatefile:'newxuejingres',//网校管理员私有属性文件
	publicuserfile:'newxuejinguser',//网校用户公共文件
};

// 初始化 oss client
export function initOssClient(accessKeyId, accessKeySecret, stsToken, expiration, bucket = "newxuejingimage") {
		client = new OSS({
			secure: true,
			accessKeyId,
			accessKeySecret,
			stsToken,
			region: 'oss-cn-beijing',
			bucket
		});
	expirationTime = expiration;
	return client;
}

// 检查 oss 实例以及过期时间
export function checkOssClient() {
	const current = moment();
	return moment(expirationTime).diff(current) < 0 ? null : client;
}

// 初始化 oss client
export function initOssVideoClient(accessKeyId, accessKeySecret, stsToken, expiration, bucket = "newxuejingres") {
		videoclient = new OSS({
			secure: true,
			accessKeyId,
			accessKeySecret,
			stsToken,
			region: 'oss-cn-beijing',
			bucket
		});
	expirationTime2 = expiration;
	return videoclient;
}


// 检查 oss 实例以及过期时间
export function checkOssVideoClient() {
	const current = moment();
	return moment(expirationTime2).diff(current) < 0 ? null : videoclient;
}

// 用于 sts token 失效、用户登出时注销 oss client
export function destroyOssClient() {
	client = null;
	videoclient=null;
}
